﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using SupermarketManager.Core;
using SupermarketManager.Model;


namespace SupermaketManager.DataAccess
{

    class NhanvienAccess
    {
        dbConnection db = new dbConnection();
        public DataTable Laytoanbonhanvien()
        {
            return db.executeSelectProcedureQueryNonParameter("select_nhanvien");
        }
        //---- Tìm kiếm theo mã nhân viên
        public DataView timkiemManv(string manv)
        {
            DataView dataView = Laytoanbonhanvien().DefaultView;
            dataView.RowFilter = string.Format("maNV like '%{0}%'", manv);
            return dataView;
        }
        //---- Tìm kiếm theo tên nhân viên 
        public DataView timkiemtennhanvien(string Tennv)
        {
            DataView dataView = Laytoanbonhanvien().DefaultView;
            dataView.RowFilter = string.Format("tenNV like '%{0}%'", Tennv);
            return dataView;
        }
        public DataTable timnhanvientheoma(string manv)
        {
            SqlParameter[] listParam = new SqlParameter[1];
            SqlParameter ma = new SqlParameter("@maNV", SqlDbType.Char, 10);
            ma.Value = manv;
            listParam[0] = ma;
            return db.executeSelectProcedureQuery("select_nhanvien_theoma", listParam);
        }
        //---- Thêm nhân viên
        public int themnhanvien(NhanVien nv)
        {

            if (timnhanvientheoma(nv.MaNV).Rows.Count > 0) return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[7];
                SqlParameter manv = new SqlParameter("@maNV", SqlDbType.VarChar, 10);
                manv.Value = 
                listParam[0] = manv;
                listParam[1] = new SqlParameter("@hodemNV", nv.HodemNV);
                listParam[2] = new SqlParameter("@tenNV", nv.HodemNV);
                listParam[3] = new SqlParameter("@gioiTinh",  nv.GioiTinh);
                listParam[4] = new SqlParameter("@diaChi",nv.DiaChi);
                listParam[5] = new SqlParameter("@soDT", nv.SoDT);
                listParam[6] = new SqlParameter("@email", nv.Email);
                db.executeInsertProcedureQuery("insert_nhanvien", listParam);
                return 1;
            }
        }
        //---- Sửa nhân viên 
        public int suanhanvien(NhanVien nv)
        {
            if ((timnhanvientheoma(nv.MaNV) == null))
                return 0;
            else
            {

                SqlParameter[] listParam = new SqlParameter[7];
                SqlParameter manv = new SqlParameter("@maNV", SqlDbType.VarChar, 10);
                manv.Value =
                listParam[0] = manv;
                listParam[1] = new SqlParameter("@hodemNV", nv.HodemNV);
                listParam[2] = new SqlParameter("@tenNV", nv.HodemNV);
                listParam[3] = new SqlParameter("@gioiTinh", nv.GioiTinh);
                listParam[4] = new SqlParameter("@diaChi", nv.DiaChi);
                listParam[5] = new SqlParameter("@soDT", nv.SoDT);
                listParam[6] = new SqlParameter("@email", nv.Email);
                db.executeInsertProcedureQuery("update_nhanvien", listParam);
                return 1;
              
            }
        }
        // Xóa nhanvien
        public int xoanv(NhanVien nv)
        {

            if ((timnhanvientheoma(nv.MaNV) == null))
                return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[1];

                SqlParameter manv = new SqlParameter("@maNV", SqlDbType.Char, 10);
                manv.Value = nv.MaNV;
                listParam[0] = manv;
                db.executeDeleteProcedureQuery("delete_nhanvien", listParam);
                return 1;
            }
        }

    }
}

    

